package com.baymax.test.scene.demo

import android.animation.ObjectAnimator
import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.animation.doOnCancel
import androidx.core.animation.doOnEnd
import androidx.recyclerview.widget.RecyclerView
import com.baymax.test.scene.BaseScene
import com.baymax.test.scene.addButton
import com.baymax.test.scene.internal.*
import com.baymax.test.scene.newScrollContainer
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import kotlin.properties.Delegates

/**
 * @title:       标题
 * @project:     TestScene
 * @package:     com.baymax.test.scene.demo
 * @class:       Type21Animator
 * @description:
 * <p>
 * 类描述
 * </p>
 *
 * @version:   1.0
 * @createDate:   2023/2/1 15:29
 * @Copyright (C) 2023 YSTEN
 * @author:       xuyujian
 */
class Type21AnimatorScene : BaseScene() {


    val gson = Gson()

    fun parse(json: String): List<Style> {
        val styles = gson.fromJson<List<Style>>(
            requireActivity().assets.open(json).bufferedReader(),
            object : TypeToken<List<Style>>() {}.type
        )
        return styles?.map { Style(it.left, it.top, it.width, it.height) } ?: emptyList()
    }

    override fun onCreateView(p0: LayoutInflater, p1: ViewGroup, p2: Bundle?): View {
        val scene = this
        return p1.newScrollContainer {
            println("newScrollContainer")
            val c = p1.context
            val rv = Module21View(c)
            var sizeIndex = 0
            var styleIndex = 0
            var styles: List<Style> by Delegates.observable(emptyList()) { p, o, n ->
                rv.adapter?.notifyDataSetChanged()
                println("notifyDataSetChanged $n")
            }
            addView(LinearLayout(c).apply {
                addButton("style1.json") {
                    styleIndex = 0
                    styles = parse("style1.json")
                }
                addButton("style2.json") {
                    styleIndex = 1
                    styles = parse("style2.json")
                }
                addButton("style3.json") {
                    styleIndex = 2
                    styles = parse("style3.json")
                }

            })
            addButton("size change $sizeIndex") {
                sizeIndex = sizeIndex.plus(1).mod(styles.size)
                this.text = "size change $sizeIndex"
            }
            addButton("scale0") {
                val anim = if (styleIndex == 0) "max1" else "max2"
                rv.adapter?.notifyItemChanged(sizeIndex, anim)
            }
            addButton("scale1") {
                val anim = if (styleIndex == 0) "min1" else "min2"
                rv.adapter?.notifyItemChanged(sizeIndex, anim)
            }
//            styles.forEach {
//                it.left += 80
//            }

            rv.childSizeLookup = ItemsSizeLookup(requireSceneContext()) { styles }

            addView(rv)
            rv.clipToPadding = false
            rv.clipChildren = false
            //rv.setPadding(80, 30, 80, 30)

            class VH(itemView: View) : RecyclerView.ViewHolder(itemView) {
                val txt = itemView as TextView

                init {
                    txt.apply {
                        minHeight = 40
                        minWidth = 40
                        textSize = 20f
                        gravity = Gravity.CENTER
                        setTextColor(Color.RED)
                        background = GradientDrawable().apply {
                            setStroke(1, Color.GREEN)
                        }
                        isFocusable = true
                        setOnFocusChangeListener { v, hasFocus ->
                            val pos = this@VH.adapterPosition
                            v.animate().scaleX(if (hasFocus) 1.2f else 1f).start()


                        }
                    }
                }
            }

            val adapter = object : RecyclerView.Adapter<VH>() {
                override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH =
                    VH(TextView(c)).apply {
                        println("onCreateViewHolder")
                    }

                override fun onBindViewHolder(holder: VH, position: Int) {
                    holder.txt.text = "P:$position"

                }

                override fun onBindViewHolder(
                    holder: VH, position: Int, payloads: MutableList<Any>
                ) {
                    super.onBindViewHolder(holder, position, payloads)
                    println("payloads $payloads")
                    payloads.forEach {
                        when (it) {
                            "request" -> holder.itemView.requestFocus()
                            "max1" -> {
                                val s = styles[position]
                                val endValues = intArrayOf(s.renderWidth, s.width * 2)
                                println("payloads max1:${endValues.contentToString()}")
                                val o = TranslationViewWrapper(
                                    holder.itemView, styles[position], styles, endValues.last()
                                )
                                ObjectAnimator.ofInt(
                                    o, "width", *endValues
                                ).setDuration(600)
                                    .apply {
                                        doOnCancel {
                                            println("doOnCancel")
                                        }
                                        doOnEnd {
                                            println(o)
                                        }
                                    }
                                    .start()
                            }

                            "min1" -> {
                                val s = styles[position]
                                val endValues = intArrayOf(s.renderWidth, s.width)
                                println("payloads min1:${endValues.contentToString()}")
                                val translationViewWrapper = TranslationViewWrapper(
                                    holder.itemView, styles[position], styles, endValues.last()
                                )
                                ObjectAnimator.ofInt(
                                    translationViewWrapper, "width", *endValues
                                ).setDuration(600).apply {
                                    doOnEnd {
                                        println(translationViewWrapper)
                                    }
                                }.start()
                            }

                            "max2" -> {
                                val s = styles[position]
                                val endValues = intArrayOf(s.renderWidth, s.width * 2)
                                println("payloads max2:${endValues.contentToString()}")
                                ObjectAnimator.ofInt(
                                    FitLineWrapper(
                                        holder.itemView, styles[position], styles
                                    ), "width", *endValues
                                ).setDuration(600).start()


                            }

                            "min2" -> {
                                val s = styles[position]
                                val endValues = intArrayOf(s.renderWidth, s.width)
                                println("payloads min2:${endValues.contentToString()}")
                                ObjectAnimator.ofInt(
                                    FitLineWrapper(
                                        holder.itemView, s, styles
                                    ), "width", *endValues
                                ).setDuration(600).start()


                            }

                            else -> println("payloads none op $it")
                        }
                    }
                }

                override fun getItemCount(): Int = styles.size
            }
            rv.adapter = adapter
            styles = parse("style1.json")
            adapter.notifyDataSetChanged()
            println("notifyDataSetChanged $styles")
        }
    }
}